package com.fuful.servlet;

import com.fuful.domain.Cart;
import com.fuful.domain.Product;
import com.fuful.domain.Users;
import com.fuful.service.CartService;
import com.fuful.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AddCartAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        //产品
        ProductService ps = new ProductService();
        Product product = ps.findByProductId(pid);
        HttpSession session = request.getSession();
        Users myUser = (Users)session.getAttribute("user");
        //购物车
        Cart myCart = new Cart(myUser,product,1);
        CartService cartService = new CartService();
        cartService.addCart(myCart);
        response.sendRedirect("/myCart.do");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

